import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWidgets import QTreeWidget, QTreeWidgetItem

class TreeWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("树形组件示例")
        self.resize(400, 300)
        
        layout = QVBoxLayout()
        
        # 创建树形组件
        tree = QTreeWidget()
        tree.setHeaderLabels(["名称", "类型", "大小"])
        
        # 添加顶级项目
        root = QTreeWidgetItem(tree)
        root.setText(0, "根项目")
        
        # 添加子项目
        child1 = QTreeWidgetItem(root)
        child1.setText(0, "子项目1")
        child1.setText(1, "文件")
        child1.setText(2, "10KB")
        
        child2 = QTreeWidgetItem(root)
        child2.setText(0, "子项目2")
        child2.setText(1, "文件夹")
        
        # 展开所有项目
        tree.expandAll()
        
        # 项目选择事件
        tree.itemClicked.connect(self.on_item_clicked)
        
        # 添加到布局
        layout.addWidget(tree)
        self.setLayout(layout)
    
    def on_item_clicked(self, item, column):
        print(f"选择了: {item.text(0)} (列 {column})")

# 使用树形窗口
app = QApplication(sys.argv)
window = TreeWindow()
window.show()
sys.exit(app.exec())
